package com.hexus.eve.actions;

import java.util.Map;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.hexus.eve.entities.Account;
import com.hexus.eve.entities.Character;
import com.hexus.eve.entities.CharacterBlueprint;
import com.hexus.eve.utils.CurrentUser;
import com.hexus.eve.utils.EmptyItems;
import com.opensymphony.xwork2.ActionSupport;

public class DisplayCharacter extends ActionSupport{

	@Autowired
	SessionFactory sessionFactory;
	
	private static final long serialVersionUID = 1L;

	private Integer id = EmptyItems.INTEGER;
	private Character character;

	 
	
	public String execute() {
		if(id != null && !EmptyItems.INTEGER.equals(id)) {
			for(Account a : CurrentUser.getUser().getAccounts()) {
				Map<Integer, Character> characters = a.getCharactersMap();
				if(characters.containsKey(id)) {
					character = characters.get(id);
					/*
					if(character.getBlueprints().size() > 0) {
						System.out.println("Character " + character.getName() + " blueprints:");
						for(CharacterBlueprint bp : character.getBlueprints()) {
							System.out.println("#" + bp.getId() + " " + bp.getName());
						}
					} else {
						HibernateTemplate ht = new HibernateTemplate(sessionFactory);
						CharacterBlueprint cb = new CharacterBlueprint();
						cb.setBlueprintID(683);
						cb.setCharacterID(15);
						cb.setName("Bantam Blueprint");
						try {
							ht.save(cb);
							character.getBlueprints().add(cb);
							//ht.update(character);
						} catch (Exception e) {
							System.out.println(e.getMessage());
						}
					}
					*/
					if(!character.isAvailabilityStatusUpToDate()) {
						character.updateAvailabilityStatus();
					}
				}
			}
		}
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Character getCharacter() {
		return character;
	}

	public void setCharacter(Character character) {
		this.character = character;
	}
	
	
}
